var AjaxRequest = function(){

    var that = this;

    //创建XMLHTTPRequest对象
    that.createXHR = function(){
        if (window.XMLHttpRequest){
            // code for IE7+, Firefox, Chrome, Opera, Safari
            return new XMLHttpRequest();
        }else if(window.ActiveXObject){
            // IE6,IE5
            return new ActiveXObject("Microsoft.XMLHTTP");
        }else{
            throw new Error("您的浏览器不支持AJAX！");
        }
    };

    //初始化数据和方法
    that.init = function(obj){
        var objAdapter = {
            method:"get",
            data:{},
            success:function(){},
            complete:function(){},
            error:function(s){
                alert('status:' + s + 'error!');
            },
            async:true
        };
        //通过使用JS随机字符串解决IE浏览器第二次默认获取缓存的问题
        that.url = obj.url + '?rand=' + Math.random();
        that.method = obj.method || objAdapter.method;
        that.data = that.params(obj.data) || that.params(objAdapter.data);
        that.async = obj.async || objAdapter.async;
        that.complete = obj.complete || objAdapter.complete;
        that.success = obj.success || objAdapter.success;
        that.error = obj.error || objAdapter.error;
    };

    //ajax异步调用
    that.ajax = function(obj){
        that.method = obj.method || 'get';
        if(that.method === "post" || that.method === "POST"){
            that.post(obj);
        }else if(that.method === "get" || that.method === "GET"){
            that.get(obj);
        }
    };

    //post方法
    that.post = function(obj){
        var xhr = that.createXHR();//创建XHR对象
        that.init(obj);
        that.method = "post";
        if(that.async===true){
            //异步
            xhr.onreadystatechange = function(){
                //响应已就绪
                if(xhr.readyState==4){
                    that.callback(obj,this); //回调函数
                }
            }
        }

        xhr.open(that.method,that.url,that.async);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.send(that.data);

        if(that.async===false){
            //同步
            that.callback(obj,this);
        }  
    }; 

    //get方法
    that.get = function(obj){
        var xhr = that.createXHR();//创建XHR对象
        that.init(obj);
        if(that.async===true){
            //异步
            xhr.onreadystatechange = function(){
                //响应已就绪
                if(xhr.readyState==4){
                    that.callback(obj,this); //回调函数
                }
            }
        }

        that.url += that.url.indexOf('?')==-1?'?'+that.data:'&'+that.data;
        xhr.open(that.method,that.url,that.async);
        xhr.send(null);
        if(that.async===false){
            //同步
            that.callback(obj,this);
        } 
    };

    //请求成功的回调函数
    that.callback = function(obj,xhr){
        if(xhr.status==200 || xhr.status==304){
            // obj.success(JSON.parse(xhr.responseText));
            obj.success(xhr.responseText);//回调传递参数
        }else{
            alert('获取数据错误！错误代号：' + xhr.status + '，错误信息：' + xhr.statusText);
        }
    }; 

    //数据转换
    that.params = function(data){
        var arr = [];
        for(var i in data){
            //特殊字符传参产生的问题可以使用encodeURIComponent()进行编码处理
            arr.push(encodeURIComponent(i) + '=' + encodeURIComponent(data[i]));
        }
        return arr.join('&');
    };

    //返回的结果
    return {
        post:that.post,
        get:that.get,
        ajax:that.ajax

    }

}

var ajaxRequest = new AjaxRequest;
//ajax请求
// ajaxRequest.ajax({
//     method:'get',
//     url:'php/test.php',
//     data:{
//         "username":"chen",
//         "password":"123456"
//     },
//     success:function(data){
//         console.log(data);
//     },
//     error:function(error){
//         console.log(error);
//     }
// });

//get请求
// ajaxRequest.get({
//     url:'php/test.php',
//     data:{
//         "username":"chen",
//         "password":"123456"
//     },
//     success:function(data){
//         console.log(data);
//     },
//     error:function(error){
//         console.log(error);
//     }
// });

//post请求
ajaxRequest.post({
    url:'php/test.php',
    data:{
        "username":"chen",
        "password":"123456"
    },
    success:function(data){
        console.log(data);
    },
    error:function(error){
        console.log(error);
    }
});